package com.fh.controller;

import com.fh.common.ResultEnum;
import com.fh.common.ResultMassage;
import com.fh.model.Role;
import com.fh.service.RoleService;
import com.fh.util.DataTablePageUtil;
import com.fh.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("initqueryRole")
    public String initqueryRole(){
        return "role/role";
    }

    @RequestMapping("queryRole")
    @ResponseBody
    public ResultMassage queryRole(DataTablePageUtil<Role> page, RoleVo roleVo){
        try{
            DataTablePageUtil<Role> roleDataTablePageUtil = roleService.queryRole(page, roleVo);
            return new ResultMassage(ResultEnum.QUERY_SUCCESS_MSG,roleDataTablePageUtil);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.QUERY_ERROR_MSG);
        }

    }

    @RequestMapping("addRole")
    @ResponseBody
    public ResultMassage addRole(Role role){
        try{
            roleService.addRole(role);
            return new ResultMassage(ResultEnum.ADD_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.ADD_ERROR_MSG);
        }
    }


    @RequestMapping("toUpdateRole")
    @ResponseBody
    public ResultMassage toUpdateRole(Integer roleId){
        try{
            Role role = roleService.toUpdateRole(roleId);
            return new ResultMassage(ResultEnum.TOUPDATE_SUCCESS_MSG,role);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.TOUPDATE_ERROR_MSG);
        }
    }


    @RequestMapping("updateRole")
    @ResponseBody
    public ResultMassage updateRole(Role role){
        try{
            roleService.updateRole(role);
            return new ResultMassage(ResultEnum.UPDATE_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.UPDATE_SUCCESS_MSG);
        }
    }

    @RequestMapping("deleteRole")
    @ResponseBody
    public ResultMassage deleteRole(Integer roleId){
        try{
            roleService.deleteRole(roleId);
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }
    }

    @RequestMapping("bothdeleteRole")
    @ResponseBody
    public ResultMassage bothdeleteRole(@RequestParam("arr[]") Integer[] arr){
        try{
            roleService.deleteBothRole(arr);
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultMassage(ResultEnum.DELETE_SUCCESS_MSG);
        }
    }
}
